<html>
<head><meta charset="utf-8"><title>ItemScope types vs PerNsGlobImports · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html">ItemScope types vs PerNsGlobImports</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221363416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221363416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221363416">(Jan 01 2021 at 18:45)</a>:</h4>
<p>Hi, </p>
<p>I was wondering what the PerNSGlobImports is used for?</p>
<p>An example I had was the following:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="k">use</span><span class="w"> </span><span class="n">bar</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">mod</span> <span class="nn">bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>In the code there are two glob imports, so my original idea was that each module/container would get a PerNsGlobImport struct to populate. This is wrong however, since there is only one PerNsGlobImport on the DefCollector.</p>



<a name="221363480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221363480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221363480">(Jan 01 2021 at 18:47)</a>:</h4>
<hr>
<p>I also noticed that PerNsGlobImport does interact with the ItemScope here, whereby which also has it's own namespace for types, values and macros: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/item_scope.rs#L203">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/item_scope.rs#L203</a></p>
<p>The above method seems to check each of the types, value, namespaces in the ItemScope and then dependending on whether the import type is Named/Glob it will remove or insert into the Glob Imports</p>



<a name="221363637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221363637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221363637">(Jan 01 2021 at 18:51)</a>:</h4>
<p>Hmm I guess the part where the dots are not connected for me, is the purpose for PerNSGlobImports and why there seems to be only one per DefCollector/Crate</p>



<a name="221364448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364448">(Jan 01 2021 at 19:10)</a>:</h4>
<p>PerNsGlobImports just keeps track of which names in each module come from a glob import, so that they can be properly shadowed by named imports. You're right that there's just one per crate, but note that each of the sets in PerNsGlobImports contains module/name combinations</p>



<a name="221364454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364454">(Jan 01 2021 at 19:11)</a>:</h4>
<p>it could be one set of names per module, but it's just more efficient (probably) to have one big set of module/name pairs</p>



<a name="221364706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364706">(Jan 01 2021 at 19:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/ItemScope.20types.20vs.20PerNsGlobImports/near/221364454">said</a>:</p>
<blockquote>
<p>it could be one set of names per module, but it's just more efficient (probably) to have one big set of module/name pairs</p>
</blockquote>
<p>Oh I see that makes sense. Thank you very much</p>
<p>Once I've done researching the function name issue. Will look into how the following is distinguished:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="err">```</span><span class="n">rust</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">foo</span>::<span class="o">*</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">world</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">bar</span>::<span class="o">*</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221364756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364756">(Jan 01 2021 at 19:18)</a>:</h4>
<p>they're not. we don't handle local imports at all yet</p>



<a name="221364821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364821">(Jan 01 2021 at 19:20)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/ItemScope.20types.20vs.20PerNsGlobImports/near/221364756">said</a>:</p>
<blockquote>
<p>they're not. we don't handle local imports at all yet</p>
</blockquote>
<p>Oh I see, when they are implemented I guess the key would be ContainedID instead of LocalModuleID or something similar</p>



<a name="221364906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364906">(Jan 01 2021 at 19:23)</a>:</h4>
<p>maybe. local imports won't be resolved within the <code>CrateDefMap</code> of the surrounding crate, there'll be a separate def map for the function</p>



<a name="221364998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221364998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221364998">(Jan 01 2021 at 19:26)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/ItemScope.20types.20vs.20PerNsGlobImports/near/221364906">said</a>:</p>
<blockquote>
<p>maybe. local imports won't be resolved within the <code>CrateDefMap</code> of the surrounding crate, there'll be a separate def map for the function</p>
</blockquote>
<p>Oh right, this makes sense to me since local imports cannot be accessed via external crates where modules can be.</p>



<a name="221365169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221365169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221365169">(Jan 01 2021 at 19:31)</a>:</h4>
<p>yes, and we don't want to look into item bodies if we don't have to. See <a href="https://github.com/rust-analyzer/rust-analyzer/issues/1165">rust-analyzer#1165</a> and <a href="https://github.com/rust-analyzer/rust-analyzer/issues/5922">rust-analyzer#5922</a> for some notes about that</p>



<a name="221365411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/ItemScope%20types%20vs%20PerNsGlobImports/near/221365411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/ItemScope.20types.20vs.20PerNsGlobImports.html#221365411">(Jan 01 2021 at 19:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/ItemScope.20types.20vs.20PerNsGlobImports/near/221365169">said</a>:</p>
<blockquote>
<p>yes, and we don't want to look into item bodies if we don't have to. See <a href="https://github.com/rust-analyzer/rust-analyzer/issues/1165">rust-analyzer#1165</a> and <a href="https://github.com/rust-analyzer/rust-analyzer/issues/5922">rust-analyzer#5922</a> for some notes about that</p>
</blockquote>
<p>Ahh right 5922 makes sense to me from what I can understand, thanks for the link</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>